package com.one.group.admin.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.MenuDTO;
import com.one.group.model.dto.RoutersMenuDto;
import com.one.group.model.entity.Menu;
import com.one.group.result.Result;
import com.one.group.service.MenuService;
import com.one.group.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * {@code @Create:}  2025-09-28  14 : 55
 * {@code @Author:} Cherry
 * {@code @ToUser:} 凡是发生,皆有利于我。
 * ------------------------------------
 * {@code @note:}
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 添加菜单
     * @param menu
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result save(@RequestBody Menu menu) {
        menu.setId(null);
        return ResultUtil.success( menuService.save(menu));
    }

    /**
     * 获取菜单树
     */
    @RequestMapping(value = "/tree",method = RequestMethod.GET)
    public Result getMenuTree() {
        //超级管理员查询所有菜单
        String loginId = (String) StpUtil.getLoginId();
        Long adminId=loginId.equals("2")?0:Long.valueOf(loginId);
//        Long adminId= AdminThreadLocal.get().getUsername().equals("admin")?0L:AdminThreadLocal.get().getId();
        List<Menu> menuList = menuService.selectMenuTree(adminId);
        if (CollectionUtils.isEmpty(menuList)){
            return ResultUtil.error(ErrorCode.FORBIDDEN_ERROR);
        }
        return ResultUtil.success(menuList);
    }


    /**
     * 获取所有菜单
     * @return
     */
    @RequestMapping(value = "/all",method = RequestMethod.GET)
    public Result getMenus() {
        //设置为0时，service不会走条件查询，会查询所有菜单
        List<Menu> menuList = menuService.selectMenuTree(0L);
        return ResultUtil.success(menuList);
    }


    /**
     * 获取路由
     * @return
     */
    @RequestMapping(value = "getRouters",method = RequestMethod.GET)
    public Result getRouters() {
        //超级管理员获取所有路由
        String loginId = (String) StpUtil.getLoginId();
        Long adminId=loginId.equals("1")?0:Long.valueOf(loginId);
//        Long adminId=AdminThreadLocal.get().getUsername().equals("admin")?0L:AdminThreadLocal.get().getId();
        List<Menu> menuList = menuService.selectMenuTree(adminId);
        return ResultUtil.success(RoutersMenuDto.buildMenus(menuList));
    }

    /**
     * 修改菜单
     * @param menuDto
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result updateMenu(@RequestBody MenuDTO menuDto) {
        menuService.updateMenuById(menuDto);
        return ResultUtil.success();
    }

    /**
     * 根据id删除菜单
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public Result deleteMenu(@PathVariable("id") Long id) {
         menuService.removeMenuById(id);
        return ResultUtil.success();
    }



    /**
     * 根据角色ID获取其所有权限ID
     * @param id role id
     */
    @RequestMapping(value = "/getRoleHasMenu",method = RequestMethod.GET)
    public Result getRoleHasMenu(Long id) {
        List<Long> allMenu = menuService.getMenuByRoleId(id);
        return ResultUtil.success(allMenu);
    }
}

